package com.json.entity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class JSONRead {

	private List<Results> listKDO;
	
	public JSONRead() {
		listKDO = new ArrayList<Results>();
	}

	public List<Results> loadJSON(String token, String id) {


		String readTwitterFeed = readTwitterFeed(token, id);
		try {
			// récupérer les objets JSON racine "state" et "results"
			JSONObject jsonObject = new JSONObject(readTwitterFeed);
			
			// si le state est en valid, ça veut dire qu'il n y a pas d'erreurs
			if (jsonObject.getString("state").equals("valid")) {
				
				// notre tableau de l'objet "results"
				JSONArray jsonArray = jsonObject.getJSONArray("results");
				
				// parcourir chaque élément et récupérer des POJO Results
				for(int i = 0; i < jsonArray.length(); i++){
					
					// élément Result
					JSONObject element = (JSONObject) jsonArray.get(i);
					listKDO.add(new Results(element.get("name").toString(), 
							element.get("description").toString(),
							element.get("url").toString(),
							element.get("imageUrl").toString(),
							element.get("price").toString()));
				}
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listKDO;
	}

	public String readTwitterFeed(String token, String id) {

		StringBuilder builder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet("http://www.weedem.fr/100pui/ctrl.php?id="+id+"&token="+token+"&platform=facebook");
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
			} else {
				System.out.println("Failed to download file");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return builder.toString();
	}

}
